/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pessoas.presenter;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import pessoas.log.ConfiguracoesLog;
import pessoas.view.ConfiguracoesLogView;

/**
 *
 * @author Bush
 */
public class ConfiguracoesLogPresenter {

    private ConfiguracoesLogView view;
    private ConfiguracoesLog configLog;

    public ConfiguracoesLogPresenter() {
        view = new ConfiguracoesLogView(null, true);
        exibeLogs();
        try {
            atualizaTipo();
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        } catch (ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        } catch (InstantiationException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        } catch (IllegalAccessException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
        view.getBtnAlterar().addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    alterar();
                } catch (ClassNotFoundException ex) {
                    JOptionPane.showMessageDialog(null, ex.getMessage());
                } catch (InstantiationException ex) {
                    JOptionPane.showMessageDialog(null, ex.getMessage());
                } catch (IllegalAccessException ex) {
                    JOptionPane.showMessageDialog(null, ex.getMessage());
                }
            }
        });

        view.getBtnFechar().addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                view.dispose();
            }
        });

        view.setLocationRelativeTo(view);
        view.setVisible(true);
    }

    public void exibeLogs() {
        try {
            ArrayList<Object> lista = ConfiguracoesLog.getInstancia().listaTiposLog();
            for(int i=0;i<lista.size();i++){
                view.getComboTipo().addItem(lista.get(i));
            }
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        } catch (ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        } catch (InstantiationException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        } catch (IllegalAccessException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }

    public void atualizaTipo() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        try {
            view.getTxtTipoAtual().setText(ConfiguracoesLog.getInstancia().getTipoLog());
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }

    public void alterar() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        try {
            ConfiguracoesLog.getInstancia().alteraTipoLog(view.getComboTipo().getSelectedItem().toString());
            JOptionPane.showMessageDialog(null, "Alterado com Sucesso!!");
            atualizaTipo();
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }

}
